package com.tool_stdy.dbPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by yan_li on 2017/1/13.
 */
public class PoolTest {
    private static PoolServiceImpl poolManager = PoolManager.getInstance();

    public static void main(String[] args) {
        try {
            for (int i = 0; i < 10; i++) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            selectData();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
//            selectData();
//            selectData();
//            selectData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void selectData() throws SQLException {
        String sql = "select * from t_grade";
        PoolConnection connectionPool = poolManager.getRelationConnection();
        Connection connection = connectionPool.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()) {
            System.out.println(Thread.currentThread().getName()+"   " + resultSet.getString("id") + "-" + resultSet.getString("name"));
        }
        connectionPool.close();
    }
}
